package com.gzbugu.bugusys.util.imp;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.util.IHQLQuery;

/**
 * HQL数据库查询
 * 
 * @author gauze
 * 
 */
@Transactional
public class HQLQuery extends HibernateDaoSupport implements IHQLQuery {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaya.util.imp.IHQLQuery#find(java.lang.String,
	 * com.yaya.bean.PageModel)
	 */
	public List find(String hql, PageModel page) {
		return this.find(hql, (page.getCurrentPage() - 1) * page.getPageSize(),
				page.getPageSize());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaya.util.imp.IHQLQuery#find(java.lang.String, int, int)
	 */
	public List find(String hql, int offset, int pagesize) {
		try {
			Query query = this.getSession().createQuery(hql);
			query.setFirstResult(offset);
			query.setMaxResults(pagesize);
			return query.list();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaya.util.imp.IHQLQuery#find(java.lang.String)
	 */
	public List find(String hql) {
		try {
			Query query = this.getSession().createQuery(hql);
			return query.list();
		} catch (Exception e) {
			return null;
		}
	}
}
